layui.use(['form','layer','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;


    	Date.prototype.Format = function (fmt) { //author: meizz   
		    var o = {  
		        "M+": this.getMonth() + 1, //月份   
		        "d+": this.getDate(), //日   
		        "H+": this.getHours(), //小时   
		        "m+": this.getMinutes(), //分   
		        "s+": this.getSeconds(), //秒   
		        "q+": Math.floor((this.getMonth() + 3) / 3), //季度   
		        "S": this.getMilliseconds() //毫秒   
		    };  
		    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));  
		    for (var k in o)  
		    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));  
		    return fmt;  
		} 
       
       rendering ();
       function rendering(){
       	 $.ajax({
            type: "GET",
            headers: {
                token :  $.cookie('tokenA')
            },
            url: api+"exam/list",
            data: {
               pageSize:100,
               pageNum:1
            },
            success: function(result){
            	tableData = result.data.list;
            	console.log(tableData);
			   //新闻列表
			    var tableIns = table.render({
			        elem: '#userList',
			        data : tableData,
			        cellMinWidth : 95,
			        page : true,
			        height : "full-125",
			        limits : [10,15,20,25],
			        limit : 20,
			        id : "userListTable",
			        cols : [[
			            {type: "checkbox", fixed:"left", width:50},
			            {field: 'topicName', title: '试题名称', align:'center'},
			            {field: 'aOptionContent', title: '选项A',  align:'center'},
			            {field: 'bOptionContent', title: '选项B',  align:'center'},
			            {field: 'cOptionContent', title: '选项C',  align:'center'},
			            {field: 'dOptionContent', title: '选项D',  align:'center'},
			            {field: 'correctOption', title: '答案',  align:'center',width:100},
			            {field: 'topicScore', title: '分值',  align:'center',width:100},
			            {title: '操作', width:170, templet:'#testListBar',fixed:"right",align:"center"}
			        ]]
			    });


                table.on('tool(testList)', function(obj){
			        var layEvent = obj.event,
			            rowData = obj.data;
			            console.log(rowData)
			        if(layEvent === 'edit'){ //编辑
			            editTest(rowData);
			        }else if(layEvent === 'del'){ //删除
			            layer.confirm('确定删除此试题？',{icon:3, title:'提示信息'},function(index){			
			                $.ajax({
					            type: "post",
					            headers: {
					                token :  $.cookie('tokenA')
					            },
					            url: api+"exam/remove/"+rowData.id,
			                    success: function(result){
			                    	console.log(result);
			                    	if(result.code == 1){
				                           layer.msg("删除成功");
								           setTimeout(function(){
						                          layer.closeAll();
						                           rendering (); 
									       },1000);
				                    }
			                    }
			                });
			
			            });
			        }
			
			
			    });
     	
			    //添加试题
			    function addTest() {
			        var index = layui.layer.open({
			            title : "添加试题",
			            type : 2,
			            area : ["700px","480px"],
			            content : "test-add.html",
			            success : function(layero, index){
			 
			            }
			        });
			    }
			    //编辑试题
			    function editTest(edit){
			        var index = layui.layer.open({
			            title : "修改试题",
			            type : 2,
			            area : ["700px","480px"],
			            content : "test-edit.html",
			            success : function(layero, index){
			                var body = layui.layer.getChildFrame('body', index);
			                if(edit){
			                    body.find(".id").val(edit.id);
			                    body.find(".testName").val(edit.topicName);
			                    body.find(".optionA").val(edit.aOptionContent);
			                    body.find(".optionB").val(edit.bOptionContent);
			                    body.find(".optionC").val(edit.cOptionContent);
			                    body.find(".optionD").val(edit.dOptionContent);
			                    body.find(".answer-list input[value="+edit.correctOption+"]").prop("checked","checked");
			                    body.find(".scoreTest").val(edit.topicScore);
			
			
			                    form.render();
			                }
			                setTimeout(function(){
			                    layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
			                        tips: 3
			                    });
			                },500)
			            }
			        });
			    }
			    $(".add-test").click(function(){
			        addTest();
			    })
                            	
            }
        });

       }
    
});